using System;
using System.Collections.Generic;
using System.Text;

namespace My.IdlConverter.Core.Util
{
    public delegate bool Unary<T>(T param1,T param2);

    public class ArrayHelper
    {
        public static string FindKeyword(string[] keywords,string valueContainKeyword)
        {
            foreach (string nextKeyword in keywords)
            {
                if (valueContainKeyword.IndexOf(nextKeyword) != -1)
                    return nextKeyword;
            }
            throw new KeyNotFoundException(String.Format("The key word can not found in {0}", valueContainKeyword));
        }

        public static int IndexOf<T>(IList<T> elems, T elem)
        {
            int index = -1;
            if (elems.Count == 0)
                return index;

            foreach (T nextElem in elems)
            {
                index++;
                if (elem.Equals(nextElem))
                {
                    return index;
                }
            }
            return -1;
        }

        public static bool FindString(string[] elems, string elem)
        {
            if (elems.Length == 0)
                return false;
            elem = elem.Trim();
            foreach (string nextElem in elems)
            {
                if (elem.Equals(nextElem.Trim()))
                {
                    return true;
                }
            }

            return false;
        }

        public static IList<T> Copy<T>(IList<T> source)
        {
            if (source == null)
                return null;
            IList<T> target = new List<T>();
            foreach (T nextElem in source)
            {
                target.Add(nextElem);
            }

            return target;
        }

        public static IList<T> FindCopies<T>(IList<T> source,T elem,Unary<T> unary)
        {
            if (source == null)
                return null;
            if (unary == null)
                throw new ArgumentNullException("The predicate method should not be null.");
            IList<T> target = new List<T>();
            foreach (T nextElem in source)
            {
                if (unary(elem,nextElem))
                {
                    target.Add(nextElem);
                }
            }

            return target;
        }

        public static void RemoveRange<T>(IList<T> remover, IList<T> removee)
        {
            foreach (T nextElem in removee)
            {
                remover.Remove(nextElem);
            }
        }

        public static void ExtractAndSortInterfaces(Class classObj,Stack<Interface> orderedInterfaceStack)
        {
            IList<Interface> baseInterfaces = classObj.BaseInterfaces;
            Interface defaultBaseInterface = classObj.DefaultBaseInterface;
            if (defaultBaseInterface == null)
                return;
            orderedInterfaceStack.Push(defaultBaseInterface);
            foreach (Interface nextInterface in baseInterfaces)
            {
                if (nextInterface.Name.Equals(defaultBaseInterface.Name))
                    continue;
                if (nextInterface.IsClass())
                {
                    Class tmpClassObj = nextInterface as Class;
                    System.Diagnostics.Debug.Assert(tmpClassObj != null);
                    ExtractAndSortInterfaces(tmpClassObj, orderedInterfaceStack);
                }
                else
                {
                    orderedInterfaceStack.Push(nextInterface);
                }
            }
        }
    }
}
